package com.example.broadcastreceiver;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private MediaPlayer mediaPlayer;
    private BatteryLevelReceiver mBatteryLevelReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //我们要收听的频道
        IntentFilter intentFilter=new IntentFilter();//创建一个意图过滤器，选择要的，其他的都过滤掉
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);//添加电池变更，这个过滤的时候会保留
        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);//添加电源连接
        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);//添加电源未连接
        mBatteryLevelReceiver = new BatteryLevelReceiver();
        //注册广播
        this.registerReceiver(mBatteryLevelReceiver,intentFilter);
        mediaPlayer = new MediaPlayer();
        Uri uri=Uri.parse("http://site.show160.com/usermusic/upmusic/20129/20120906033132236.mp3");
        try {
            mediaPlayer.setDataSource("http://site.show160.com/usermusic/upmusic/20129/20120906033132236.mp3");
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static final String TAG = "MainActivity";
    /**
     * 创建一个胳膊接收者的子类,在后台完美运行
     */
    private class BatteryLevelReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();//获取过滤到的东西
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                Log.d(TAG, "收到的电量变化广播 -- action is onReceive: "+action);
                int intExtra = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                Log.d(TAG, "onReceive: 当前电量"+intExtra);
                if (intExtra==99){
                    //播放电量为99的音乐
                    Log.d(TAG, "onReceive: 电量快充满了");
                    mediaPlayer.start();
                }else if (intExtra==100){
                    //播放电量为100的音乐
                    Log.d(TAG, "onReceive: 电量已经充满了");
                    mediaPlayer.start();
                }
            }
            if (action.equals(Intent.ACTION_POWER_CONNECTED)){
                Toast.makeText(context, "USB已经连接了", Toast.LENGTH_SHORT).show();
            }else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)){
                Toast.makeText(context, "USB未插入", Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //释放播放资源
        if (mediaPlayer!=null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
        //取消广播注册
        if (mBatteryLevelReceiver!=null) {
            this.unregisterReceiver(mBatteryLevelReceiver);
        }
    }
}